/**
 * Created by yqj on 2017/2/3.
 */
import { userResource } from '../../api'

export const userManagementStore = {
  state: {
    user_role: null,
    queryUserList: [],
    activeType: null,
    itemsPerPage: 10,
    currentPage: 1,
    totalSize: 0,
    items: {},
    users: {},
    lists: []
  },
  actions: {
    FETCH_USER_LIST_DATA ({ commit, dispatch, state }, { role, type, filterAccountPrefix }) {
      return userResource.getUserByRole({filterRole: role || 'SUPER_ADMIN', currentPage: state.currentPage, pageSize: state.itemsPerPage, filterAccountPrefix: filterAccountPrefix})
        .then(response => {
          let list = response.body.result.content
          let totalSize = response.body.result.totalSize
          commit('SET_USER_LIST_DATA', { type, list })
          commit('SET_TOTAL_SIZE', { totalSize })
        })
    },
    FETCH_QUERY_USER_LIST ({ commit, dispatch, state }, { role }) {
      return userResource.getQueryUserList({ role })
        .then(response => {
          let list = response.body.result.content
          commit('SET_QUERY_USER_LIST', { list })
        })
    },
    CHANGE_ITEMS_PER_PAGE ({ commit, dispatch, state }, { pageSize }) {
      commit('SET_ITEMS_PER_PAGE', { pageSize })
      commit('SET_CURRENT_PAGE', { currentPage: 1 })
    },
    CHANGE_CURRENT_PAGE ({ commit, dispatch, state }, { currentPage }) {
      commit('SET_CURRENT_PAGE', { currentPage })
    },
    CREATE_USER ({ commit, dispatch, state }, data) {
      return userResource.createUser({loginPassword: data.loginPassword}, { ...data.body })
    },
    DELETE_USER ({ commit, dispatch, state }, item) {
      return userResource.deleteUser({deletingUserId: item.id || item._id})
    }
  },
  mutations: {
    SET_USER_LIST_DATA (state, { type, list }) {
      state.lists = list
    },
    SET_TOTAL_SIZE (state, { totalSize }) {
      state.totalSize = totalSize
    },
    SET_ITEMS_PER_PAGE (state, { pageSize }) {
      state.itemsPerPage = pageSize
    },
    SET_CURRENT_PAGE (state, { currentPage }) {
      state.currentPage = currentPage
    },
    SET_QUERY_USER_LIST (state, { list }) {
      state.queryUserList = list
    }
  },
  getters: {

  }
}
